package vt.ece4564.phonetracker;

import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


/**
 * @author BMW
 *
 */
public class RulesActivity extends Activity implements OnClickListener {
	public static final String PREFS_NAME = "PhoneTrackerPrefsFile";

	Button newRule, savePhone;
	Vector<String> rules = new Vector<String>();
	String allrules = "";

	EditText phone_;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rules);

		newRule = (Button) findViewById(R.id.newRuleButton);
		newRule.setOnClickListener(this);
		
		savePhone = (Button) findViewById(R.id.savePhoneNumber);
		savePhone.setOnClickListener(this);
		((Button) findViewById(R.id.returnToHome)).setOnClickListener(this);

		Intent intent = getIntent();
		if (intent.hasExtra("allrules")) {
			allrules = intent.getStringExtra("allrules");
			List<String> list = Arrays.asList(allrules.split("&"));
			rules = new Vector<String>(list);
			// String rule = rules.get(rules.size()-1);
			Toast toast = Toast.makeText(getApplicationContext(), "New Rule!",
					Toast.LENGTH_LONG);
			toast.show();

			printRules();
		} else {
			System.out.println("Loading From Shared Preference...");
			SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
			allrules = settings.getString("allrules", "");
			System.out.println("..." + allrules);
			List<String> list = Arrays.asList(allrules.split("&"));
			rules = new Vector<String>(list);
			printRules();
		}
		phone_ = (EditText) findViewById(R.id.editPhone);
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		String alertphone = settings.getString("alertphone", "");
		phone_.setText(alertphone);
		System.out.println("Saved phone=" + alertphone);
	}

	@Override
	protected void onStop() {
		System.out.println("MAPS Stopping");
		super.onStop(); 
		// Some seed rules
		// allrules =
		// "School%500%37213730%-80460055%7:30%14:15%Mon-Tue-Wed-Thu-Fri&Music Lessons%200%37229587%-80417900%15:0%16:0%Tue-Thu";
		// List<String> list = Arrays.asList(allrules.split("&"));
		// rules = new Vector<String>(list);
		saveRules();
	}

	protected void saveRules() {
		// We need an Editor object to make preference changes.
		// All objects are from android.context.Context
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("allrules", allrules);
		// Commit the edits!
		editor.commit();
	}

/*	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_hello_google_maps, menu);
		return true;
	}*/

	@Override
	public void onClick(View v) {
		System.out.println("Button Click: " + v.getTag());
		if (v.getTag() != null) {
			System.out.println("Rules List Size: " + rules.size());
			rules.removeElementAt((Integer) v.getTag());
			printRules();
		}
		switch (v.getId()) {
		case (R.id.newRuleButton):
			Intent intent = new Intent(this, AddRuleMapActivity.class);
			intent.putExtra("allrules", allrules);
			startActivity(intent);
			break;
		case (R.id.returnToHome):
			Intent intent2 = new Intent(this, Settings.class);
			startActivity(intent2);
			break;
		case (R.id.savePhoneNumber):
			String result = "";
			String alertphone = phone_.getText().toString();
			if (alertphone.length() == 10) {
				SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
				SharedPreferences.Editor editor = settings.edit();
				editor.putString("alertphone", alertphone);
				editor.commit();
				result+="Number Saved!";
			} else {
				result += "Number needs to be 10 digits";
			}
			Toast toast = Toast.makeText(getApplicationContext(), result,
					Toast.LENGTH_LONG);
			toast.show();
			stopService(new Intent(this,PhoneFinderService.class));
			Intent serviceIntent = new Intent(this,PhoneFinderService.class);
			startService(serviceIntent);
			break;
		}
	}

	public void printRules() {
		LinearLayout rulesList = (LinearLayout) findViewById(R.id.listRules);
		rulesList.removeAllViews();

		System.out.println("Rules List Size: " + rules.size());
		allrules = "";
		for (int i = 0; i < rules.size(); i++) {
			String rule = rules.get(i);

			System.out.println(rule);
			String[] strs = rule.split("%");
			System.out.println("Size of strs: " + strs.length);
			if (strs.length == 7) {
				allrules += rule;
				if (i + 1 < rules.size())
					allrules += "&";
				Button b = new Button(this);
				b.setTag(i);
				b.setText("Clear");
				b.setOnClickListener(this);

				TextView tv = new TextView(this);
				String tvcontent = "";
				tvcontent += strs[0] + "\n";
				tvcontent += "Radius: " + strs[1] + "\n";
				tvcontent += (double) Integer.parseInt(strs[2]) / 1e6 + ", "
						+ (double) Integer.parseInt(strs[3]) / 1e6 + "\n";
				tvcontent += strs[4] + " to " + strs[5] + "\n";
				tvcontent += strs[6] + "\n";

				strs = strs[6].split("-");
				for (int j = 0; j < strs.length; j++) {
					System.out.println("Day:" + j + "=" + strs[j]);
				}
				tv.setText(tvcontent);
				LinearLayout linlay = new LinearLayout(this);
				linlay.setOrientation(LinearLayout.HORIZONTAL);
				linlay.addView(b);
				linlay.addView(tv);

				rulesList.addView(linlay);
			}
		}
		saveRules();
		stopService(new Intent(this,PhoneFinderService.class));
		Intent serviceIntent = new Intent(this,PhoneFinderService.class);
		startService(serviceIntent);
	}

}
